package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.WorkshopMapper;
import com.ruoyi.cderp.domain.Workshop;
import com.ruoyi.cderp.service.IWorkshopService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 车间Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-09
 */
@Service
public class WorkshopServiceImpl implements IWorkshopService
{
    @Autowired
    private WorkshopMapper workshopMapper;

    LoginUser loginUser = null;

    /**
     * 查询车间
     *
     * @param id 车间主键
     * @return 车间
     */
    @Override
    public Workshop selectWorkshopById(String id)
    {
        return workshopMapper.selectWorkshopById(id);
    }

    /**
     * 查询车间列表
     *
     * @param workshop 车间
     * @return 车间
     */
    @Override
    public List<Workshop> selectWorkshopList(Workshop workshop)
    {
        return workshopMapper.selectWorkshopList(workshop);
    }

    /**
     * 新增车间
     *
     * @param workshop 车间
     * @return 结果
     */
    @Override
    public int insertWorkshop(Workshop workshop)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formattedDate = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
        workshop.setId("16" + formattedDate+randomNumber);
        workshop.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        workshop.setCreateBy(loginUser.getUser().getUserName());
        workshop.setCreateTime(DateUtils.getNowDate());
        return workshopMapper.insertWorkshop(workshop);
    }

    /**
     * 修改车间
     *
     * @param workshop 车间
     * @return 结果
     */
    @Override
    public int updateWorkshop(Workshop workshop)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        workshop.setUpdateBy(loginUser.getUser().getUserName());
        workshop.setUpdateTime(DateUtils.getNowDate());
        return workshopMapper.updateWorkshop(workshop);
    }

    /**
     * 批量删除车间
     *
     * @param ids 需要删除的车间主键
     * @return 结果
     */
    @Override
    public int deleteWorkshopByIds(String[] ids)
    {
        return workshopMapper.deleteWorkshopByIds(ids);
    }

    /**
     * 删除车间信息
     *
     * @param id 车间主键
     * @return 结果
     */
    @Override
    public int deleteWorkshopById(String id)
    {
        return workshopMapper.deleteWorkshopById(id);
    }
}
